package com.chenyue.cm.utils.MD5;

public class Encrypts {
	
	public static final int HASH_ITERATIONS = 1024;
	private static final int SALT_SIZE = 8;
	
	private Encrypts(){}
	
	/**
	 * 生成安全哈希码. 
	 * @param plain 明文密码
	 * @return string[0]:HASH安全密码，string[1]:SALT值.
	 */
	public static String[] hashPassword(String plain) {
		byte[] salt = Digests.generateSalt(SALT_SIZE);
		String hexSalt = Encodes.encodeHex(salt);

		byte[] hash = Digests.sha1(plain.getBytes(), salt, HASH_ITERATIONS);
		String hexHash = Encodes.encodeHex(hash);
		
		return new String[]{hexHash, hexSalt};
	}
	// APP-Crm登陆
	public static String encodeCrmPassword(String plain,String salt) {
		return hashPassword(plain, salt);
	}
	
	/**
	 * 生成安全哈希码. 
	 * @param plain 明文密码
	 * @param salt 16进制盐值
	 * @return HASH安全密码.
	 */
	public static String hashPassword(String plain, String salt) {
		byte[] saltBytes = Encodes.decodeHex(salt);
		byte[] hash = Digests.sha1(plain.getBytes(), saltBytes, HASH_ITERATIONS);
		return Encodes.encodeHex(hash);
	}

}
